/**
 * 
 * @authors 3pixel (kermit@piexl.net)
 * @date    2017-11-23 10:21:18
 * @version v0.0.1
 */

//依赖jquery
var Home = function() {
	var  home = this;

	//幻灯片初始化
	home.bannerSwiperInit = function(){
		var $banner = $('#banner'),
			$pagination = $banner.find('.swiper-pagination');
		home.bannerSwiper = new Swiper($banner, {
			autoplay: 5000,
			effect:'fade',
			pagination: $pagination,
			paginationClickable :true,
			paginationBulletRender: function (swiper, index, className) {
				var _index = (index + 1)>9?index+1:'0'+(index+1);
				return '<span class="' + className + '">' + _index + '</span>';
			}
		})
	}
	//与海达路德同行
	home.peerSwiperInit = function(){
		var $peerSwiper = $('#peerSwiper'),
			$nextBtn = $peerSwiper.find('.peer-button-next'),
			$prevBtn = $peerSwiper.find('.peer-button-prev'),
			swiperLenght = $peerSwiper.find('.swiper-slide').length;
			headerHeight = $('#header').outerHeight();
		if(Common.screenWidth>=768){
			$peerSwiper.height(Common.screenHeight-headerHeight);
		}else{
			$peerSwiper.height('auto');
		}
		home.peerSwiper = new Swiper($peerSwiper, {
			loop:true,
			speed:500,
			loopedSlides:home.peerSwiperLenght,
			centeredSlides:true,
			slidesPerView:'auto',
			simulateTouch:false,
			prevButton:$prevBtn,
			nextButton:$nextBtn,
			paginationClickable :true,
			onInit:function(swiper){
				$peerSwiper.find(".swiper-slide").click(function(event){
					if(!$(this).hasClass('swiper-slide-active')){
						var $this = $(this),
							index = $this.index(),
							slideIndex = parseInt($this.data('swiper-slide-index')),
							activeIndex = $peerSwiper.find('.swiper-slide-active').index();
						//console.log('index',index,'activeIndex',activeIndex);
						var diff = activeIndex-index,
							diffAbs = Math.abs(diff);
						//console.log('diff',diff,'diffAbs',diffAbs);
						//模拟点击
						for(var i = 0; i < diffAbs; i++){
							if(i< diffAbs-1){
								if(diff>0){
									home.peerSwiper.slidePrev(function(){}, 0);
								}else{
									home.peerSwiper.slideNext(function(){}, 0);
								}
							}else{
								if(diff>0){
									home.peerSwiper.slidePrev(function(){}, 500);
								}else{
									home.peerSwiper.slideNext(function(){}, 500);
								}
								
							}
						}
					}
				});
				$peerSwiper.find(".img-link").click(function(event) {
					var $this = $(this),
						canGo = $this.parents('.swiper-slide').hasClass('swiper-slide-active'),
						href = $this.data('href');
					if(canGo){window.location.href = href;}
				});
			},
			breakpoints: {
		   		768:{
		   	   		slidesPerView: 1,
		   	 	}
			},
		})
	}
	//短途体验
	home.ShortExpInit = function(){
		home.ShortExpIndex = 0;
		var $shortExp = $('#shortExp'),
			$nextBtn = $shortExp.find('.shortExp-button-next'),
			$prevBtn = $shortExp.find('.shortExp-button-prev'),
			headerHeight = $('#header').outerHeight();
		if(Common.screenWidth>=1024){
			$shortExp.height(Common.screenHeight-headerHeight);
			$shortExp.find('.short-exp-item').hover(function(){
				var $this = $(this),
					index = $(this).index();
				home.ShortExpIndex = index;
				$this.addClass('short-exp-item-active').siblings('.short-exp-item').removeClass('short-exp-item-active');
			});
		}else{
			$shortExp.height('auto');
		}
	}
	//体验
	home.experienceSwiperInit = function(){
		var $experienceSwiper = $('#experienceSwiper'),
			$nextBtn = $experienceSwiper.find('.experience-button-next'),
			$prevBtn = $experienceSwiper.find('.experience-button-prev');
		home.experienceSwiper = new Swiper($experienceSwiper, {
			//loop:true,
			//centeredSlides : true,
			slidesOffsetBefore : 0,
			slidesPerView:3,
			breakpoints: {
			  	1024: {
			    	slidesPerView: 2,
			  	},
			  	// 768:{
			   //   	slidesPerView: 2,
			   // 	},
		   		480:{
		   	   		slidesPerView: 1,
		   	 	}
			},
			prevButton:$prevBtn,
			nextButton:$nextBtn
		})
	}
	//下载
	home.downloadSwiperInit = function(){
		var $downloadSwiper = $('#downloadSwiper'),
			$pagination = $downloadSwiper.find('.swiper-pagination');
			$swiperSlideLength = $downloadSwiper.find('.swiper-slide').lenght;
		if($swiperSlideLength<3){
			$pagination.remove();
		}
		home.downloadSwiper = new Swiper($downloadSwiper, {
			slidesPerView:3,
			spaceBetween: 20,
			paginationClickable :true,
			pagination: $pagination,
			breakpoints: {
			  	768:{
			     	spaceBetween: 10,
			   	}
			},
		})
	}
	//页面滚动
	home.ScrollIng = function(){
		$(window).scroll(function(event) {
			var $header = $('#header');
				scrollTop = $(this).scrollTop(),
				headerHeight = $header.outerHeight();
			if(scrollTop>headerHeight){
				$header.addClass('header-white');
			}else{
				$header.removeClass('header-white');
			}
		});
	}
	//页面窗口变化
	home.WindowResize = function(){
		$(window).resize(function(event){
			home.peerSwiper.destroy(false,true);
			home.peerSwiperInit();
		});
	}
	home.Init = function() {
		home.bannerSwiperInit();
		home.peerSwiperInit();
		home.experienceSwiperInit();
		home.downloadSwiperInit();
		home.ScrollIng();
		home.WindowResize();
		home.ShortExpInit();
		console.log('home init')
	}
}
var Home = new Home();
Home.Init();
